import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
// @ts-ignore - 忽略vuex类型问题
import { createStore } from 'vuex'

import App from './App.vue'
import HomeView from './views/HomeView.vue'
import UsageGuideView from './views/UsageGuideView.vue'
import MedicationsView from './views/MedicationsView.vue'
import InteractionsView from './views/InteractionsView.vue'
import PatientInfoView from './views/PatientInfoView.vue'
import RiskAssessmentView from './views/RiskAssessmentView.vue'
import DrugRecommendationsView from './views/DrugRecommendationsView.vue'
import DoctorReviewView from './views/DoctorReviewView.vue'

// 创建路由
const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', component: HomeView },
    { path: '/usage-guide', component: UsageGuideView },
    { path: '/medications', component: MedicationsView },
    { path: '/interactions', component: InteractionsView },
    { path: '/patient-info', component: PatientInfoView },
    { path: '/risk-assessment', component: RiskAssessmentView },
    { path: '/drug-recommendations', component: DrugRecommendationsView },
    { path: '/doctor-review', component: DoctorReviewView },
  ]
})

interface Medication {
  id: number
  name: string
  dosage: string
  frequency: string
  route: string
}

interface PatientInfo {
  age: number
  weight: number
  gender: string
  liverFunction: string
  kidneyFunction: string
  allergies: string[]
}

// 创建状态管理
const store = createStore({
  state() {
    return {
      medications: [],
      patientInfo: {
        age: 65,
        weight: 70,
        gender: 'male',
        liverFunction: 'normal',
        kidneyFunction: 'mild',
        allergies: ['青霉素']
      },
      interactions: []
    }
  },
  
  mutations: {
    // @ts-ignore - 忽略vuex类型问题
    ADD_MEDICATION(state, medication: Medication) {
      state.medications.push(medication)
    },
    // @ts-ignore - 忽略vuex类型问题
    REMOVE_MEDICATION(state, medicationId) {
      state.medications = state.medications.filter((m: Medication) => m.id !== medicationId)
    },
    
    // @ts-ignore - 忽略vuex类型问题
    UPDATE_PATIENT_INFO(state, patientInfo: PatientInfo) {
      state.patientInfo = { ...state.patientInfo, ...patientInfo }
    },
    
    // @ts-ignore - 忽略vuex类型问题
    SET_INTERACTIONS(state, interactions) {
      state.interactions = interactions
    }
  },
  
  actions: {
    // @ts-ignore - 忽略vuex类型问题
    addMedication({ commit }, medication: Medication) {
      commit('ADD_MEDICATION', medication)
    },
    
    // @ts-ignore - 忽略vuex类型问题
    removeMedication({ commit }, medicationId: number) {
      commit('REMOVE_MEDICATION', medicationId)
    },
    
    // @ts-ignore - 忽略vuex类型问题
    updatePatientInfo({ commit }, patientInfo: PatientInfo) {
      commit('UPDATE_PATIENT_INFO', patientInfo)
    },
    
    // @ts-ignore - 忽略vuex类型问题
    checkInteractions({ commit, state }) {
      // 模拟API调用
      const interactions = [
        {
          id: 1,
          drugs: ['阿司匹林', '华法林'],
          severity: 'high',
          description: '增加出血风险，可能导致严重出血事件',
          recommendation: '考虑调整剂量或更换药物，加强监测'
        }
      ]
      commit('SET_INTERACTIONS', interactions)
    }
  }
})

// 创建并挂载应用
const app = createApp(App)

app.use(router)
app.use(store)

app.mount('#app')